package com.zhouyibike.uu.ui.activity.mybike;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.WeekIncomingResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Zy on 2017/10/11.
 */
public class MyBikeFragment extends Fragment {
    private String tokenn;
    private int type = 0;
    private BarChart mChart;
    RelativeLayout jiazaily;
    LinearLayout nodataly;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        View view = View.inflate(getActivity(), R.layout.fragment_mybike, null);
        //  type=getIntent().getStringExtra("type");
        tokenn = b.getString("token");
        type = b.getInt("type");
        initView(view);
        initCharts(view);
        jiazaily.setVisibility(View.VISIBLE);
        if (type == 0) {
            getMsg2(tokenn);
        } else {
            getMsg3(tokenn);
        }

        //  getMsg();
        // ((BaseActivity) getActivity()).toastShow(b.getString("type"));
        return view;
    }

    private void initView(View view) {
        jiazaily=view.findViewById(R.id.jiazaily);
        nodataly=view.findViewById(R.id.nodataly);
    }

    private void initCharts(View view) {
        mChart = (BarChart) view.findViewById(R.id.chart1);
        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.setDescription("");
        mChart.setMaxVisibleValueCount(60);
        mChart.setPinchZoom(false);
        mChart.setDrawGridBackground(false);
        mChart.setTouchEnabled(false);
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //    xAxis.setTypeface(mTf);
        xAxis.setDrawGridLines(false);
        xAxis.setSpaceBetweenLabels(2);
        xAxis.setTextColor(getResources().getColor(R.color.radio_button_selected_color));
        xAxis.setTextSize(10f);
        xAxis.setAxisLineWidth(1f);
        xAxis.setAxisLineColor(getResources().getColor(R.color.bg_main_white));
        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setTextSize(0f);
        leftAxis.setGridColor(Color.WHITE);
        leftAxis.setDrawGridLines(true);
        leftAxis.setLabelCount(8, false);
        leftAxis.setAxisLineWidth(1f);
        leftAxis.setAxisLineColor(Color.WHITE);
        leftAxis.setTextSize(0f);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMinValue(0f);
        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setDrawGridLines(false);
        rightAxis.setLabelCount(8, false);
        rightAxis.setTextColor(Color.WHITE);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMinValue(0f);
        rightAxis.setEnabled(false);
        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);
        l.setEnabled(false);
//        setData(7, 100);

    }

    private void getMsg2(String token) {
        Map<String, String> params = new HashMap<>();
        params.put("token", token);
        ((BaseActivity) (getActivity())).addSubscription(((BaseActivity) (getActivity())).apiStores.WeekIncomingForTenBike(params),
                new ApiCallback<WeekIncomingResult>() {

                    @Override
                    public void onSuccess(WeekIncomingResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            setData(model);
                        } else {
//                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onFinish() {
                        jiazaily.setVisibility(View.GONE);
                        // getMsg3();
                    }
                });
    }

    private void getMsg3(String token) {
        Map<String, String> params = new HashMap<>();
        params.put("token", token);
        ((BaseActivity) (getActivity())).addSubscription(((BaseActivity
                ) (getActivity())).apiStores.WeekIncomingForMyBike(params),
                new ApiCallback<WeekIncomingResult>() {

                    @Override
                    public void onSuccess(WeekIncomingResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            try {
//                                ((MyBikeActivity5) (getActivity())).zuorishouyi.setText(model.getData().get(model.getData().size() - 1).getAmount() + "");
                                ((MyBikeinfo1Fragment)getParentFragment()).zuorishouyitv.setText(model.getData().get(model.getData().size() - 1).getAmount() + "");
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                setData(model);
                            }
                        } else {
                            //  toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onFinish() {
                        //   dismissProgressDialog();
                        jiazaily.setVisibility(View.GONE);
                    }
                });
    }

    private void setData(WeekIncomingResult model) {
        ArrayList<String> xVals = new ArrayList<String>();
//        ArrayList<String> strings = getsevenday();
        if (model==null||model.getData()==null||model.getData().size()==0)
        {
            nodataly.setVisibility(View.VISIBLE);
        }else{
            nodataly.setVisibility(View.GONE);
        }
        for (int i = 0; i < model.getData().size(); i++) {
            xVals.add(model.getData().get(i).getDay() + "");
        }
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < model.getData().size(); i++) {
            float val = (float) model.getData().get(i).getAmount();
            yVals1.add(new BarEntry(val, i));
        }
        BarDataSet set1;
        set1 = new BarDataSet(yVals1, "DataSet");
        set1.setBarSpacePercent(60f);
        set1.setColor(getResources().getColor(R.color.radio_button_selected_color));
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        mChart.setData(data);
        mChart.animateXY(0, 500);
        for (IDataSet set : mChart.getData().getDataSets()) {
            set.setValueTextColor(getResources().getColor(R.color.radio_button_selected_color));
        }
        mChart.invalidate();
        mChart.setVisibility(View.VISIBLE);
    }
    private void setnullData(WeekIncomingResult model) {
        ArrayList<String> xVals = new ArrayList<String>();
//        ArrayList<String> strings = getsevenday();
        for (int i = 0; i < model.getData().size(); i++) {
            xVals.add(model.getData().get(i).getDay() + "");
        }
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        for (int i = 0; i < model.getData().size(); i++) {
            float val = (float) model.getData().get(i).getAmount();
            yVals1.add(new BarEntry(val, i));
        }
        BarDataSet set1;
        set1 = new BarDataSet(yVals1, "DataSet");
        set1.setBarSpacePercent(50f);
        set1.setColor(getResources().getColor(R.color.radio_button_selected_color));
        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);
        BarData data = new BarData(xVals, dataSets);
        data.setValueTextSize(10f);
        mChart.setData(data);
        mChart.animateXY(0, 500);
        for (IDataSet set : mChart.getData().getDataSets()) {
            set.setValueTextColor(getResources().getColor(R.color.radio_button_selected_color));
        }
        mChart.invalidate();
        mChart.setVisibility(View.VISIBLE);
    }
}
